﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web
{
    public partial class htmlhelper : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string type = "F";
            switch (type)
            {
                case "A":
                    A();
                    break;
                case "B":
                    B();
                    break;
                case "C":
                    C();
                    break;
                case "D":
                    D();
                    break;
                case "E":
                    E();
                    break;
                case "F":
                    F();
                    break;
                default: break;
            }
        }
        //GetTextByLink
        private void A()
        {
            string temp = "<a href=\"http://www.cnblogs.com/\" title=\"程序员的网上家园\"><img src=\"http://static.cnblogs.com/images/logo_small.gif\" alt=\"博客园Logo\" width=\"142\" height=\"55\"/></a>";
            temp = MSL.Utility.Html.HtmlHelper.Instance.GetTextByLink(temp);
        }
        //GetHtml
        private void B()
        {
            string html = MSL.Utility.Html.HtmlHelper.Instance.GetHtml("http://www.cnblogs.com/ChengDong/archive/2012/08/08/2628242.html");
        }
        //GetFiles
        private void C()
        {
            string html = MSL.Utility.Html.HtmlHelper.Instance.GetHtml("http://www.cnblogs.com/gnielee/archive/2010/08/02/wpf-cpu-usage.html");
            List<String[]> list = MSL.Utility.Html.HtmlHelper.Instance.GetFiles(null, ref html, null);
        }
        private void D()
        {
            string html = MSL.Utility.Html.HtmlHelper.Instance.GetHtml("http://www.cnblogs.com/gnielee/archive/2010/08/02/wpf-cpu-usage.html");
        }
        private void E()
        {
            string html = MSL.Utility.Html.HtmlHelper.Instance.GetHtml("http://govinfonew.nlc.gov.cn/gtfz/govShow2014-01-4728447-TC0000000000.htm");
            //string result = MSL.Utility.Html.HtmlHelper.GetElementValue("txttype", "", html);

        }

        protected void F()
        {

            //Dictionary<string, string> Headers = GetHTTPResponseHeaders("http://www.cnblogs.com/#p3");


        }

        /*
        private void GetRequsetData()
        {
            isPost = this.tsmPost.Checked;
            isGet = this.tsmGet.Checked;
            try
            {
                byte[] input = BitConverter.GetBytes(1);
                byte[] buffer = new byte[4096];
                Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
                s.Bind(new IPEndPoint(IPAddress.Parse(this.IP), 0));
                s.IOControl(IOControlCode.ReceiveAll, input, null);
                int bytes = 0;

                string domain = FormatWebSite(this.txtUrl.Text);
                string reg = "";
                if (isPost && !isGet)
                {
                    reg = "(?s)POST";
                }
                else if (!isPost && isGet)
                {
                    reg = "(?s)GET";
                }
                else if (isPost && isGet)
                {
                    reg = "(?s)(GET|POST)";
                }
                do
                {
                    if (s.Available < 1)
                        break;

                    bytes = s.Receive(buffer);
                    if (bytes > 0)
                    {
                        // 把发送信息显示出来，这里只显示了包含GET或POST的数据报，
                        string packet = Encoding.UTF8.GetString(buffer, 0, bytes);
                        if (packet.IndexOf(domain) == -1)
                        {
                            break;
                        }
                        Match m = Regex.Match(packet, reg);
                        if (m.Success)// && packet.IndexOf("HTTP/1") != -1)
                        {
                            SetText(this.rtbOut, "-----------------------------------");
                            SetText(this.rtbOut, packet.Substring(m.Index));
                        }
                    }
                } while (bytes > 0);
            }
            catch (Exception ex)
            {
                SetText(this.rtbOut, ex.ToString());
            }
        }
        */

        /// <summary>
        /// 返回 HTTP headers.
        /// </summary>
        /// <param name="Url">地址</param>
        /// <returns>headers的列表</returns>
        public Dictionary<string, string> GetHTTPResponseHeaders(string url)
        {
            Dictionary<string, string> HeaderList = new Dictionary<string, string>();

            WebClient x = new WebClient();
            x.Headers.Set("Timeout", "6000"); //超时设置6秒
            string source = x.DownloadString(url);

            //用正则表达式取页面标题
            string title = Regex.Match(source, @"<titleb[^>]*>s*(?<Title>[sS]*?)</title>", RegexOptions.IgnoreCase).Groups["Title"].Value;
            HeaderList.Add("Address", url); //加入地址
            HeaderList.Add("Title", title); //加入页面标题

            foreach (string HeaderKey in x.ResponseHeaders)
                HeaderList.Add(HeaderKey, x.ResponseHeaders[HeaderKey]);

            return HeaderList;
        }
    }
}